#include "MyVector.h"
#include <iostream>

template <typename T>
MyVector<T>::MyVector(int size){//构造函数
        m_space = new T[size];
        m_len = size;
} 

template <typename T>
MyVector<T>::MyVector(const MyVector<T> &obj){//拷贝构造函数
        //根据obj的大小分配内存
        m_len = obj.m_len;
        m_space = new T[m_len];
        
        //copy数据
        for (int i = 0; i < m_len; i++)
        {
                m_space[i] = obj.m_space[i];
        }
}

template <typename T>
MyVector<T>::~MyVector(){//析够函数
        if(m_space != NULL){
                delete [] m_space;
                m_space = NULL;
                m_len = 0;
        }
}

template <typename T>
T& MyVector<T>::operator[](int index){
        return m_space[index];
}

template <typename T>
MyVector<T>& MyVector<T>::operator=(const MyVector<T> &obj){
        //先把旧的内存释放掉
        if(m_space != NULL){
                delete [] m_space;
                m_space = NULL;
                m_len = 0;
        }
        //根据obj分配内存
        m_len = obj.m_len;
        m_space = new T[m_len];
        
        //copy数据
        for (int i = 0; i < m_len; i++)
        {
                m_space[i] = obj[i];
        }
        return *this;
}